package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.util.Jsons;
import com.atguigu.gmall.product.dto.ValueSkuJsonDTO;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class SpuSaleAttrServiceImpl extends ServiceImpl<SpuSaleAttrMapper, SpuSaleAttr>
    implements SpuSaleAttrService{

    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;

    /**
     * 根据spuId获取销售属性
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        return list;
    }

    /**
     * 根据skuid查询销售属性的名和值并且标记
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrListAndMark(Long spuId,Long skuId) {
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrListAndMark(spuId,skuId);
        return list;
    }

    /**
     * 根据spuid查询出这个spu下面所有的sku涉及到的所有属性组合
     * @param spuId
     * @return
     */
    @Override
    public String getSpuValueSkuJson(Long spuId) {
        List<ValueSkuJsonDTO> jsonDTOList = spuSaleAttrMapper.getSpuValueSkuJson(spuId);
        //将list集合转换成json字符串
        Map<String,Long> map = new HashMap<>();
        jsonDTOList.stream().forEach(item -> {
            map.put(item.getAttrValueConcat(),item.getSkuId());
        });

        String tostr = Jsons.tostr(map);
        return tostr;
    }
}




